# encoding: utf-8
""" 
@version: v1.0 
@author: autumner 
@license: Apache Licence  
@contact: 18322313385@163.com 
@site:  
@software: PyCharm 
@file: class_collections.py 
@time: 2019/6/24 15:07
"""
'''
目标：自定义容器
通过collections、numbers等抽像基类，可以实现自定义容器
bisect模块是一个在排序列表中插入元素的高效方式
'''
# Example use
import collections
import bisect

class SortedItems(collections.Sequence):
    def __init__(self, initial=None):
        self._items = sorted(initial) if initial is not None else []

    # Required sequence methos
    def __getitem__(self, item):
        return self._items[item]

    def __len__(self):
        return len(self._items)

    # Method for adding an item in the right location
    def add(self, item):
        bisect.insort(self._items, item)


items = SortedItems([5, 1, 3, 2])
print(list(items))
print(items[0], items[-1])
items.add(2)
print(list(items))
print(isinstance(items, collections.Iterable))
print(isinstance(items, collections.Sequence))
print(isinstance(items, collections.Container))
print(isinstance(items, collections.Sized))
print(isinstance(items, collections.Mapping))